# Gannet OS - Kernel Entry Sequence

# This is the assembly portion of the multiboot, so differs from the bootloader
# its been designed to run with GRUB (but should work with other bootloaders
# that support multiboot)

# This is part of the Gannet OS, For copy right and licensing see LICENSE in the 
# root folder, or visit http://gannet.org.uk/license/

# These are magic constants for the multiboot header (taken from osdev.org)
.set ALIGN,    1<<0             # align loaded modules on page boundaries
.set MEMINFO,  1<<1             # provide memory map
.set FLAGS,    ALIGN | MEMINFO  # this is the Multiboot 'flag' field
.set MAGIC,    0x1BADB002       # 'magic number' lets bootloader find the header
.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot

# Magic Header stuff for multiboot - taken from osdev.org. This 
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

# Implement a new stack as the bootlader could have left ESP pointing anywhere
# 16Kib should be ok to start with.
.section .bootstrap_stack
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

# See linker script - this requires there to be a _start position which is
# where the kernel will be entered. GRUB (or whatever boot loader) will jump
# to this position after loading into memory.
.section .text
.global _start
.type _start, @function

_start:
	#  Move the stack pointer to the kernel - (prepare to jump into C)
	movl $stack_top, %esp

	# This is expecting in kernel.c there to be a kernel_main entry poind
	call kernel_main

	# The kernel should *NOT* return - but in case it does, 
	# CLI will clear interupt, and HLT halts until the next interupt
	# if it does continue, the infinite loop will stop it doing anything dogy
	# like executing random bits of dat after the loop.
	
	cli
	hlt
.loop_forever:
	jmp .loop_forever

# its recommended to set the size here to help with debugging later
.size _start, . - _start
